package com.spic.business.accident.interevents.rectificationplan.service.impl;

import com.spic.business.accident.interevents.rectificationplan.domain.InternalEventsCounterMeasures;
import com.spic.business.accident.interevents.rectificationplan.domain.InternalIncidentRectificationPlan;
import com.spic.business.accident.interevents.rectificationplan.mapper.InternalIncidentRectificationPlanMapper;
import com.spic.business.accident.interevents.rectificationplan.service.IInternalIncidentRectificationPlanService;
import com.spic.business.common.core.workflow.IWorkFlowType;
import com.spic.business.common.workflow.WorkFlowServiceImpl;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 内部事件整改计划Service业务层处理
 * 
 * @author 任翔
 * @date 2021-12-01
 */
@Service
public class InternalIncidentRectificationPlanServiceImpl extends WorkFlowServiceImpl<InternalIncidentRectificationPlanMapper, InternalIncidentRectificationPlan> implements IInternalIncidentRectificationPlanService
{
    @Autowired
    private InternalIncidentRectificationPlanMapper internalIncidentRectificationPlanMapper;

    /**
     * 查询内部事件整改计划
     * 
     * @param id 内部事件整改计划主键
     * @return 内部事件整改计划
     */
    @Override
    public InternalIncidentRectificationPlan selectInternalIncidentRectificationPlanById(Long id)
    {
        return internalIncidentRectificationPlanMapper.selectInternalIncidentRectificationPlanById(id);
    }

    /**
     * 查询内部事件整改计划列表
     * 
     * @param internalIncidentRectificationPlan 内部事件整改计划
     * @return 内部事件整改计划
     */
    @Override
    public List<InternalIncidentRectificationPlan> selectInternalIncidentRectificationPlanList(InternalIncidentRectificationPlan internalIncidentRectificationPlan)
    {
        return internalIncidentRectificationPlanMapper.selectInternalIncidentRectificationPlanList(internalIncidentRectificationPlan);
    }

    /**
     * 新增内部事件整改计划
     * 
     * @param internalIncidentRectificationPlan 内部事件整改计划
     * @return 结果
     */
    @Transactional
    @Override
    public Long insertInternalIncidentRectificationPlan(InternalIncidentRectificationPlan internalIncidentRectificationPlan)
    {
        internalIncidentRectificationPlan.setCreateTime(DateUtils.getNowDate());
        int rows = internalIncidentRectificationPlanMapper.insertInternalIncidentRectificationPlan(internalIncidentRectificationPlan);
        insertInternalEventsCounterMeasures(internalIncidentRectificationPlan);
        return internalIncidentRectificationPlan.getId();
    }

    /**
     * 修改内部事件整改计划
     * 
     * @param internalIncidentRectificationPlan 内部事件整改计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInternalIncidentRectificationPlan(InternalIncidentRectificationPlan internalIncidentRectificationPlan)
    {
        internalIncidentRectificationPlan.setUpdateTime(DateUtils.getNowDate());
        internalIncidentRectificationPlanMapper.deleteInternalEventsCounterMeasuresByPlanId(internalIncidentRectificationPlan.getId());
        insertInternalEventsCounterMeasures(internalIncidentRectificationPlan);
        return internalIncidentRectificationPlanMapper.updateInternalIncidentRectificationPlan(internalIncidentRectificationPlan);
    }

    /**
     * 批量删除内部事件整改计划
     * 
     * @param ids 需要删除的内部事件整改计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInternalIncidentRectificationPlanByIds(Long[] ids)
    {
        internalIncidentRectificationPlanMapper.deleteInternalEventsCounterMeasuresByIds(ids);
        return internalIncidentRectificationPlanMapper.deleteInternalIncidentRectificationPlanByIds(ids);
    }

    /**
     * 删除内部事件整改计划信息
     * 
     * @param id 内部事件整改计划主键
     * @return 结果
     */
    @Override
    public int deleteInternalIncidentRectificationPlanById(Long id)
    {
        internalIncidentRectificationPlanMapper.deleteInternalEventsCounterMeasuresByPlanId(id);
        return internalIncidentRectificationPlanMapper.deleteInternalIncidentRectificationPlanById(id);
    }

    /**
     * 新增内部事件整改计划反措措施与班长关联信息
     * 
     * @param internalIncidentRectificationPlan 内部事件整改计划对象
     */
    public void insertInternalEventsCounterMeasures(InternalIncidentRectificationPlan internalIncidentRectificationPlan)
    {
        List<InternalEventsCounterMeasures> internalEventsCounterMeasuresList = internalIncidentRectificationPlan.getInternalEventsCounterMeasuresList();
        Long id = internalIncidentRectificationPlan.getId();
        if (StringUtils.isNotNull(internalEventsCounterMeasuresList))
        {
            List<InternalEventsCounterMeasures> list = new ArrayList<InternalEventsCounterMeasures>();
            for (InternalEventsCounterMeasures internalEventsCounterMeasures : internalEventsCounterMeasuresList)
            {
                internalEventsCounterMeasures.setPlanId(id);
                list.add(internalEventsCounterMeasures);
            }
            if (list.size() > 0)
            {
                internalIncidentRectificationPlanMapper.batchInternalEventsCounterMeasures(list);
            }
        }
    }

    @Override
    protected IWorkFlowType getWorkFlowType() {
        return null;
    }

    @Override
    protected void setWorkFlowVariable(InternalIncidentRectificationPlan entity, Map<String, Object> variable) {

    }
}
